package com.app.service.impl;

import com.app.mapper.jiuquan.SpecialityManageMapper;
import com.app.mapper.jiuquan.TrainMapper;
import com.app.mapper.jiuquan.TrainPeopleMapper;
import com.app.model.SpecialityManageModel;
import com.app.model.TrainModel;
import com.app.model.TrainPeopleModel;
import com.app.service.TrainPeopleService;
import com.app.vo.TrainPeopleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//培训学员报名
@Service
public class TrainPeopleServiceImpl extends ServiceImpl<TrainPeopleMapper, TrainPeopleModel> implements TrainPeopleService {
    @Autowired
    private TrainPeopleMapper trainPeopleMapper;

    @Autowired
    private SpecialityManageMapper specialityManageMapper;

    @Autowired
    private TrainMapper trainMapper;

    //所有培训学员列表
    @Override
    public TrainPeopleVo getTrainPeopleList(Integer page, Integer limit) {
        Page<TrainPeopleModel> page1 = new Page<>(page, limit);

        Page<TrainPeopleModel> trainPeoplePage = trainPeopleMapper.selectPage(page1, null);
        List<TrainPeopleModel> trainPeopleList = trainPeoplePage.getRecords();
        for (TrainPeopleModel trainPeople : trainPeopleList) {
            //专业ID转汉字
            Integer specialityId = trainPeople.getSpecialityId();
            SpecialityManageModel speciality = specialityManageMapper.selectById(specialityId);
            trainPeople.setSpecialityText(speciality.getName());
            //培训机构ID转汉字
            Integer trainId = trainPeople.getTrainId();
            TrainModel train = trainMapper.selectById(trainId);
            trainPeople.setTrainText(train.getTrainInstitution());
            //培训类型转汉字
            if (trainPeople.getTrainType() == 1) {
                trainPeople.setTrainTypeText("创业培训");
            }
            if (trainPeople.getTrainType() == 2) {
                trainPeople.setTrainTypeText("技能培训");
            }
            trainPeople.setOpenId(null);
        }
        long count = trainPeoplePage.getTotal();

        TrainPeopleVo trainPeopleVo = new TrainPeopleVo();
        trainPeopleVo.setList(trainPeopleList);
        trainPeopleVo.setCount((int) count);
        return trainPeopleVo;
    }
    //详情
    @Override
    public TrainPeopleModel getUserTrainById(Integer id) {
        TrainPeopleModel trainPeople = trainPeopleMapper.selectById(id);
        Integer specialityId = trainPeople.getSpecialityId();
        SpecialityManageModel speciality = specialityManageMapper.selectById(specialityId);
        trainPeople.setSpecialityText(speciality.getName());

        Integer trainId = trainPeople.getTrainId();
        TrainModel train = trainMapper.selectById(trainId);
        trainPeople.setTrainText(train.getTrainInstitution());

        if (trainPeople.getTrainType() == 1) {
            trainPeople.setTrainTypeText("创业培训");
        }
        if (trainPeople.getTrainType() == 2) {
            trainPeople.setTrainTypeText("技能培训");
        }
        trainPeople.setOpenId(null);
        return trainPeople;
    }

    //查询学校的培训学员列表
    @Override
    public TrainPeopleVo getTrainPeopleByTrain(Integer page, Integer limit, Integer trainId) {
        Page<TrainPeopleModel> page1 = new Page<>(page, limit);
        QueryWrapper<TrainPeopleModel> qw = new QueryWrapper<>();
        qw.eq("train_id", trainId);

        Page<TrainPeopleModel> trainPeoplePage = trainPeopleMapper.selectPage(page1, qw);
        List<TrainPeopleModel> trainPeopleList = trainPeoplePage.getRecords();
        for (TrainPeopleModel trainPeople : trainPeopleList) {
            //专业ID转汉字
            Integer specialityId = trainPeople.getSpecialityId();
            SpecialityManageModel speciality = specialityManageMapper.selectById(specialityId);
            trainPeople.setSpecialityText(speciality.getName());
            //培训机构ID转汉字
            Integer trainId2 = trainPeople.getTrainId();
            TrainModel train = trainMapper.selectById(trainId2);
            trainPeople.setTrainText(train.getTrainInstitution());
            //培训类型转汉字
            if (trainPeople.getTrainType() == 1) {
                trainPeople.setTrainTypeText("创业培训");
            }
            if (trainPeople.getTrainType() == 2) {
                trainPeople.setTrainTypeText("技能培训");
            }

            trainPeople.setOpenId(null);
        }
        long count = trainPeoplePage.getTotal();
        TrainPeopleVo trainPeopleVo = new TrainPeopleVo();
        trainPeopleVo.setList(trainPeopleList);
        trainPeopleVo.setCount((int) count);
        return trainPeopleVo;
    }

    //查询用户报名培训列表
    @Override
    public TrainPeopleVo getUserTrainList(Integer page, Integer limit, String openId) {
        Page<TrainPeopleModel> page1 = new Page<>(page, limit);
        QueryWrapper<TrainPeopleModel> qw = new QueryWrapper<>();
        qw.eq("open_id", openId);
        Page<TrainPeopleModel> trainPeoplePage = trainPeopleMapper.selectPage(page1, qw);
        List<TrainPeopleModel> trainPeopleList = trainPeoplePage.getRecords();
        for (TrainPeopleModel trainPeople : trainPeopleList) {
            //专业ID转汉字
            Integer specialityId = trainPeople.getSpecialityId();
            SpecialityManageModel speciality = specialityManageMapper.selectById(specialityId);
            trainPeople.setSpecialityText(speciality.getName());
            //培训机构ID转汉字
            Integer trainId = trainPeople.getTrainId();
            TrainModel train = trainMapper.selectById(trainId);
            trainPeople.setTrainText(train.getTrainInstitution());
            //培训类型转汉字
            if (trainPeople.getTrainType() == 1) {
                trainPeople.setTrainTypeText("创业培训");
            }
            if (trainPeople.getTrainType() == 2) {
                trainPeople.setTrainTypeText("技能培训");
            }
            trainPeople.setOpenId(null);
        }
        long count = trainPeoplePage.getTotal();

        TrainPeopleVo trainPeopleVo = new TrainPeopleVo();
        trainPeopleVo.setList(trainPeopleList);
        trainPeopleVo.setCount((int) count);
        return trainPeopleVo;
    }
}
